package com.pk.android.view;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.pk.android.R;
import com.pk.android.dummy.temp_source;
import com.pk.android.model.BaseFragment;
import com.pk.android.view.adapter.MyTeamsAdapter;

public class CreateLeagueFragment extends BaseFragment {
 
	public static final String EXTRA_TITLE = "EXTRA_TITLE";
	
	MyTeamsAdapter teamsAdapter;
	Button btnAllLeagues;

	public static final CreateLeagueFragment newInstance(String title)
	{
		CreateLeagueFragment f = new CreateLeagueFragment();
		Bundle bdl = new Bundle(1);
	   bdl.putString(EXTRA_TITLE, title);
	   f.setArguments(bdl);

		return f;
	}
    	
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
    	super.onCreateView(inflater, container, savedInstanceState);
    	
        view = inflater.inflate(R.layout.fragment_pk_create_league, container, false);

		final ListView listViewTeams = (ListView)view.findViewById(R.id.team_list);
		final EditText name = (EditText)view.findViewById(R.id.txt_league_name);
		teamsAdapter = new MyTeamsAdapter(getActivity(), temp_source.getMyTeamList());
		listViewTeams.setAdapter(teamsAdapter);
		
		listViewTeams.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
		listViewTeams.setOnItemClickListener(new OnItemClickListener(){

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				
//				int selectedPos = listViewTeams.getCheckedItemPosition();
//				if(selectedPos == arg2)
//					listViewTeams.setItemChecked(selectedPos, false);
//				else
//					listViewTeams.setItemChecked(arg2, true);
				
			}
			
		});
		
        ((Button)view.findViewById(R.id.btn_create)).setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				String name = ((TextView)view.findViewById(R.id.txt_league_name)).getText().toString().trim();
				if(name.length() > 0)
				{
					//mCallback.onCreateLeagueClick(true);
					//((EditText)view.findViewById(R.id.txt_league_name)).setEnabled(false);
					
					// TODO better to add the enable/disable footer buttons to the base activity ?
					//ChangeFooterState(false);
				}
			}
			
		});
        
        ((Button)view.findViewById(R.id.btn_cancel)).setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				name.setText("");
				listViewTeams.setItemChecked(listViewTeams.getCheckedItemPosition(), false);
				
			}
		});
    	
        ((EditText)view.findViewById(R.id.txt_league_name)).addTextChangedListener(new TextWatcher(){

			@Override
			public void afterTextChanged(Editable arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1,
					int arg2, int arg3) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onTextChanged(CharSequence arg0, int arg1, int arg2,
					int arg3) {
				// TODO Auto-generated method stub
			}
        	
        });
        
        
        return view;
    }

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// initialize essential components of the fragment that you want to retain when the fragment is paused or stopped, then resumed.
		super.onCreate(savedInstanceState);
	}


	@Override
	public void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}

	public void ChangeFooterState(boolean setEnabled)
	{
		((Button)view.findViewById(R.id.btn_create)).setEnabled(setEnabled);
        ((Button)view.findViewById(R.id.btn_cancel)).setEnabled(setEnabled);
	}
}
